package com.kongge.formlike.service.check;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * author:kongge
 * date:2018/7/20
 * layout:
 * description:
 */

public class CheckView implements ICheckItem {

    protected View view;

    protected OnCheckItemClickListener onCheckItemClickListener;

    @Override
    public String getCheckContent() {
        if (view instanceof RadioGroup) {
            RadioGroup radioGroup = (RadioGroup) view;
            int id = radioGroup.getCheckedRadioButtonId();
            RadioButton rb = radioGroup.findViewById(id);
            if (rb == null) {
                return null;
            }
            return rb.getText().toString();
        } else if (view instanceof TextView) {
            TextView textView = (TextView) view;
            return textView.getText().toString();
        }
        return null;
    }

    @Override
    public void setView(View view) {
        this.view = view;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void setOnCheckItemClickListener(OnCheckItemClickListener listener) {
        this.onCheckItemClickListener = listener;
        if (view != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onCheckItemClickListener != null) {
                        onCheckItemClickListener.onCheckItemClick(CheckView.this);
                    }
                }
            });
        }
    }
}
